package com.zhengshang.web.controller.manage;

import java.util.List;

import com.zhengshang.common.enums.UserStatus;
import com.zhengshang.common.utils.SecurityUtils;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.zhengshang.common.annotation.Log;
import com.zhengshang.common.core.controller.BaseController;
import com.zhengshang.common.core.domain.AjaxResult;
import com.zhengshang.common.enums.BusinessType;
import com.zhengshang.manage.domain.PlatformRebate;
import com.zhengshang.manage.service.IPlatformRebateService;
import com.zhengshang.common.utils.poi.ExcelUtil;
import com.zhengshang.common.core.page.TableDataInfo;

/**
 * 平台返佣Controller
 * 
 * @author zhengshang
 * @date 2021-03-18
 */
@RestController
@RequestMapping("/rebate/rebate")
public class PlatformRebateController extends BaseController
{
    @Autowired
    private IPlatformRebateService platformRebateService;

    /**
     * 查询平台返佣列表
     */
    @PreAuthorize("@ss.hasPermi('rebate:rebate:list')")
    @GetMapping("/list")
    public TableDataInfo list(PlatformRebate platformRebate)
    {
        startPage();
        List<PlatformRebate> list = platformRebateService.selectPlatformRebateList(platformRebate);
        return getDataTable(list);
    }

    /**
     * 导出平台返佣列表
     */
    @PreAuthorize("@ss.hasPermi('rebate:rebate:export')")
    @Log(title = "平台返佣", businessType = BusinessType.EXPORT)
    @GetMapping("/export")
    public AjaxResult export(PlatformRebate platformRebate)
    {
        List<PlatformRebate> list = platformRebateService.selectPlatformRebateList(platformRebate);
        ExcelUtil<PlatformRebate> util = new ExcelUtil<PlatformRebate>(PlatformRebate.class);
        return util.exportExcel(list, "rebate");
    }

    /**
     * 获取平台返佣详细信息
     */
    @PreAuthorize("@ss.hasPermi('rebate:rebate:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id)
    {
        return AjaxResult.success(platformRebateService.selectPlatformRebateById(id));
    }

    /**
     * 新增平台返佣
     */
    @PreAuthorize("@ss.hasPermi('rebate:rebate:add')")
    @Log(title = "平台返佣", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody PlatformRebate platformRebate)
    {
        if (platformRebateService.queryDateCount(platformRebate) > 0) {
            return AjaxResult.error("该平台此月份数据已经录入过！");
        }
        platformRebate.setCreateBy(SecurityUtils.getUsername());
        return toAjax(platformRebateService.insertPlatformRebate(platformRebate));
    }

    /**
     * 修改平台返佣
     */
    @PreAuthorize("@ss.hasPermi('rebate:rebate:edit')")
    @Log(title = "平台返佣", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody PlatformRebate platformRebate)
    {
        if (platformRebateService.queryDateCount(platformRebate) > 0) {
            return AjaxResult.error("该平台此月份数据已经录入过！");
        }
        platformRebate.setUpdateBy(SecurityUtils.getUsername());
        return toAjax(platformRebateService.updatePlatformRebate(platformRebate));
    }

    /**
     * 删除平台返佣
     */
    @PreAuthorize("@ss.hasPermi('rebate:rebate:remove')")
    @Log(title = "平台返佣", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids)
    {
        int count = 0;
        PlatformRebate platformRebate;
        for (Long id : ids) {
            platformRebate = new PlatformRebate();
            platformRebate.setId(id);
            platformRebate.setDelFlag(UserStatus.DELETED.getCode());
            platformRebate.setUpdateBy(SecurityUtils.getUsername());
            count = count + platformRebateService.updatePlatformRebate(platformRebate);
        }
        return toAjax(count);
    }
}
